<?php
/*
 * Created on 23 mars 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */

class ModelUser extends Model{
	function logout(){
		$_SESSION["USER_BEAN"] = null;
		return true;
	}
	function login(){
		$bool = false;
		$usrDao = $this->daoFactory->getUserDAO();
		$usrBean = $usrDao->getByEmail($this->params["email"]);
		$_SESSION["formErr"]="";
		if ($_SESSION["failedlogin"]>2){
			echo "failed >2";
			$securimg = new Securimage();
			$captchabool = $securimg->check($_POST['captcha_code']);
		}
	
		if(md5($this->params["password"])==$usrBean->getPwsHash() and ($usrBean != null) and $usrBean->isActive()and ($_SESSION["failedlogin"]<=2 or ($_SESSION["failedlogin"] >2 and $captchabool))){
	
			$_SESSION['formHasErr']=false;
			unset($_SESSION["failedlogin"]);
			$_SESSION["USER_BEAN"] = $usrBean;
			$bool=true;
		}else{
	
			$_SESSION['formHasErr']=true;
 			$_SESSION['formErr'].="<li>".$this->translator->autherr."</li>";
 			if ($_SESSION["failedlogin"]>2 and !$captchabool){
 				$_SESSION['formErr'].="<li>".$this->translator->invalidcaptcha."</li>";	
 			}
 			if (isset($_SESSION["failedlogin"])){
 				$_SESSION["failedlogin"] = $_SESSION["failedlogin"]+1;
 			}else{
 				$_SESSION["failedlogin"]=1;
 			}
 			$bool=false;
		}
		return $bool;
	}
	function showForm(){
		if(!isset($_SESSION["formErr"])){
		$_SESSION["formErr"]="";
		$_SESSION["formHasErr"]=false;
		$_SESSION["formArray"]= Array();
		}
		return "model content";
	}
	function forgotPws(){
		return "model content";
	}
	function showLogin(){
		return "model content";
	}
	function registerUser(){
		$usrBean=null;
		try{
			$usrDao = $this->daoFactory->getUserDAO();
			$usrBean = new User(0);
			$usrBean->setBoite($this->params["boite"]);
			$usrBean->setCp($this->params["cp"]);
			$usrBean->setDdn($this->params["day"],$this->params["month"],$this->params["year"]);
			$usrBean->setEmail($this->params["email"]);
			$usrBean->setIp($_SERVER['REMOTE_ADDR']);
			$usrBean->setLangue($this->daoFactory->getLangueDAO()->getById((int)$this->params["langue"]));
			$usrBean->setNom($this->params["nom"]);
			$usrBean->setNumero($this->params["numero"]);
			$usrBean->setPays($this->params["pays"]);
			$usrBean->setPrenom($this->params["prenom"]);
			$usrBean->setProfile($this->daoFactory->getProfileDAO()->getByName("webuser"));
			$usrBean->setPseudo($this->params["pseudo"]);
			if(Validator::validatePassword($this->params["pws"])){
				$usrBean->setPwsHash(md5($this->params["pws"]),md5($this->params["confirm"]));
			}else{
				throw new FieldFormatException("Password must be formated properly: minimum 6 char");
			}
			
			$usrBean->setRue($this->params["rue"]);
			$usrBean->setVille($this->params["ville"]);
			$_SESSION['count']=1;
			$usrBean=$usrDao->add($usrBean);
			if($usrBean==null){
				$_SESSION["formHasErr"]=true;	
			}else{
				$_SESSION["formHasErr"]=false;
				$_SESSION["formErr"]="";
				$_SESSION["USER_BEAN"]=$usrBean;
				$_SESSION['count']=2;
			}
		}catch (FieldFormatException $e){
			$usrBean=null;
			$_SESSION["formHasErr"]=true;
			while($key = key($this->params)){
				$_SESSION["formArray"][$key]=$this->params[$key];
				next($this->params);
			}
			$_SESSION["formErr"]=$e->getMessage();
		}
		return $usrBean;
		
	}
}
?>
